package com.koron.order.patrol.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.koron.bean.query.PageQuery;
import com.koron.order.patrol.bean.po.PatrolUserPo;
import com.koron.order.patrol.mapper.PatrolUserMapper;
import com.koron.order.patrol.service.PatrolUserService;
import org.apache.commons.collections4.CollectionUtils;
import org.koron.ebs.mybatis.SessionFactory;
import org.koron.ebs.mybatis.TaskAnnotation;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 巡视工单-巡视人员从表(PatrolUser)表服务实现类
 *
 * @author zhouj
 * @since 2021-10-26 15:28:22
 */
@Service("patrolUserService")
public class PatrolUserServiceImpl implements PatrolUserService {

    @Override
    @TaskAnnotation("insertOrUpdate")
    public int insertOrUpdate(SessionFactory factory, PatrolUserPo patrolUser) {

        PatrolUserMapper mapper = factory.getMapper(PatrolUserMapper.class);

        return mapper.insertOrUpdate(patrolUser);
    }

    @Override
    @TaskAnnotation("insertOrUpdateBatch")
    public int insertOrUpdateBatch(SessionFactory factory, List<PatrolUserPo> patrolUserList) {

        if (CollectionUtils.isEmpty(patrolUserList)) {
            return 0;
        }
        List<String> patrolIds = patrolUserList.stream().map(PatrolUserPo::getPatrolId).distinct().collect(Collectors.toList());

        Assert.notEmpty(patrolIds, "巡视工单id不能为空");

        PatrolUserMapper mapper = factory.getMapper(PatrolUserMapper.class);
        //先根据 巡视工单ids删除
        mapper.deleteByPatrolIds(patrolIds);

        return mapper.insertBatch(patrolUserList);
    }

    @Override
    @TaskAnnotation("insert")
    public int insert(SessionFactory factory, PatrolUserPo patrolUser) {

        PatrolUserMapper mapper = factory.getMapper(PatrolUserMapper.class);

        return mapper.insert(patrolUser);
    }

    @Override
    @TaskAnnotation("insertBatch")
    public int insertBatch(SessionFactory factory, List<PatrolUserPo> patrolUserList) {

        PatrolUserMapper mapper = factory.getMapper(PatrolUserMapper.class);

        return mapper.insertBatch(patrolUserList);
    }

    @Override
    @TaskAnnotation("update")
    public int update(SessionFactory factory, PatrolUserPo patrolUser) {
        PatrolUserMapper mapper = factory.getMapper(PatrolUserMapper.class);
        return mapper.update(patrolUser);
    }

    @Override
    @TaskAnnotation("deleteById")
    public int deleteById(SessionFactory factory, String id) {

        PatrolUserMapper mapper = factory.getMapper(PatrolUserMapper.class);
        return mapper.deleteById(id);
    }

    @Override
    @TaskAnnotation("queryById")
    public PatrolUserPo queryById(SessionFactory factory, String id) {

        PatrolUserMapper mapper = factory.getMapper(PatrolUserMapper.class);

        return mapper.queryById(id);
    }

    @Override
    @TaskAnnotation("queryList")
    public List<PatrolUserPo> queryList(SessionFactory factory, PatrolUserPo patrolUser) {

        PatrolUserMapper mapper = factory.getMapper(PatrolUserMapper.class);

        List<PatrolUserPo> list = mapper.queryList(patrolUser);

        return list;
    }

    @Override
    @TaskAnnotation("queryByPatrolId")
    public List<PatrolUserPo> queryByPatrolId(SessionFactory factory, String patrolId) {
        PatrolUserPo patrolUserPo = new PatrolUserPo();
        patrolUserPo.setPatrolId(patrolId);
        return queryList(factory, patrolUserPo);
    }

    @Override
    public List<PatrolUserPo> queryByPatrolIds(SessionFactory factory, List<String> patrolIds) {
        PatrolUserMapper mapper = factory.getMapper(PatrolUserMapper.class);

        List<PatrolUserPo> list = mapper.queryByPatrolIds(patrolIds);

        return list;
    }

    @Override
    @TaskAnnotation("queryPageList")
    public PageInfo queryPageList(SessionFactory factory, PatrolUserPo patrolUser, PageQuery pageQuery) {

        PageHelper.startPage(pageQuery.getPage(), pageQuery.getPageSize(), pageQuery.getOrderBy());
        PatrolUserMapper mapper = factory.getMapper(PatrolUserMapper.class);

        List<PatrolUserPo> list = mapper.queryList(patrolUser);

        return new PageInfo(list);
    }

}